<template>
  <div>
    <h1>文章列表</h1>
    <el-table :data="lists">
      <el-table-column prop="_id" label="ID" width="300"></el-table-column>
      <el-table-column prop="title" label="名称" width="200"></el-table-column>
      <!-- <el-table-column prop="parent.name" label="上级分类" width="200"></el-table-column> -->
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="$router.push(`/articles/edit/${scope.row._id}`)" type="text" size="small">编辑</el-button>
          <el-button type="primary" size="small" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      lists: [],
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    async fetch() {
      const res = await this.axios.get('rest/articles');
      this.lists = res.data;
    },
    del(item) {
      this.$confirm(`是否要删除分类--${item.name}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await this.axios.delete(`rest/articles/${item._id}`);
        this.fetch();
        this.$message({
          type: 'success',
          message: '删除成功!',
        });
      });
    },
  },
};
</script>
